import { request } from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // pics: ['../../imgs/food-t1.jpg', '../../imgs/food-t2.jpg', '../../imgs/food-t3.jpg'],
    goodsObj: {},
    specsList: [],
    pics: []
  },

  handlePrevieImage(ev) {
    let urls = this.data.pics
    // let current = ev.currentTarget.dataset.url 
    let current = ev.currentTarget.dataset.url
    wx.previewImage({
      urls,
      current
    })
  },
  goodsId: '',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { goodsId } = options
    this.goodsId = goodsId
    this.getGoodsObj(goodsId)
  },
  //将商品加入购物车
  handleAddToCart(e) {
    const { goodsObj } = this.data
    const item = {
      id: parseInt(this.goodsId),
      recommended: goodsObj.goodsPick ? 'recommended' : '',
      thumb: 'https://ma.yzlsoft.cn/static/' + goodsObj.goodsPictureMain,
      price: goodsObj.goodsPriceDiscount,
      originPrice: goodsObj.goodsPriceOriginal,
      volume: goodsObj.goodsSalesVolume,
      extra: '特价 限1份',
      num: goodsObj.goodsSalesVolume,
      title: goodsObj.goodsName,
      desc: goodsObj.goodsSummary
    }
    this.cart.addToCart(item)
    const cart = wx.getStorageSync("cart");
    console.log(cart)
  },
  //获取商品详情数据
  async getGoodsObj(goodsId) {
    const token = wx.getStorageSync("token");
    const header = { token }
    const res = await request({ url: '/fresh/goods/' + goodsId + '/showList', data: { goodsId }, header })
    let pics = res.data.goodsPicture.split(',')
    pics = pics.map(item => "https://ma.yzlsoft.cn/static/" + item)
    this.setData({
      goodsObj: res.data,
      specsList: res.data.specsList,
      pics
    })
    console.log(this.data.goodsObj)
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    this.cart = this.selectComponent('#cart')

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})